package com.video.web.controller.video;

import com.video.common.exception.ServiceException;
import com.video.video.domain.DUser;
import com.video.video.domain.dto.DSubgiftRecordDto;
import com.video.video.domain.vo.DSubgiftRecordQuery;
import com.video.video.service.IDUserService;
import java.util.List;
import javax.servlet.http.HttpServletResponse;
import org.apache.commons.lang3.StringUtils;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.video.common.annotation.Log;
import com.video.common.core.controller.BaseController;
import com.video.common.core.domain.AjaxResult;
import com.video.common.enums.BusinessType;
import com.video.video.domain.DSubgiftRecord;
import com.video.video.service.IDSubgiftRecordService;
import com.video.common.utils.poi.ExcelUtil;
import com.video.common.core.page.TableDataInfo;

/**
 * 合伙人转增记录Controller
 *
 * @author ruoyi
 * @date 2022-09-27
 */
@RestController
@RequestMapping("/video/DSubgiftRecord")
public class DSubgiftRecordController extends BaseController
{
    @Autowired
    private IDSubgiftRecordService dSubgiftRecordService;
    @Autowired
    private IDUserService idUserService;

    /**
     * 查询合伙人转增记录列表
     */
    @PreAuthorize("@ss.hasPermi('video:DSubgiftRecord:list')")
    @GetMapping("/list")
    public TableDataInfo list(DSubgiftRecordQuery query)
    {
        startPage();
        List<DSubgiftRecordDto> list = dSubgiftRecordService.selectDSubgiftRecordList(query);
        return getDataTable(list);
    }

    /**
     * 导出合伙人转增记录列表
     */
    @PreAuthorize("@ss.hasPermi('video:DSubgiftRecord:export')")
    @Log(title = "合伙人转增记录", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, DSubgiftRecordQuery query)
    {
        List<DSubgiftRecordDto> list = dSubgiftRecordService.selectDSubgiftRecordList(query);
        ExcelUtil<DSubgiftRecordDto> util = new ExcelUtil<DSubgiftRecordDto>(DSubgiftRecordDto.class);
        util.exportExcel(response, list, "合伙人转增记录数据");
    }

    /**
     * 获取合伙人转增记录详细信息
     */
    @PreAuthorize("@ss.hasPermi('video:DSubgiftRecord:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") String id)
    {
        return AjaxResult.success(dSubgiftRecordService.selectDSubgiftRecordById(id));
    }

    /**
     * 新增合伙人转增记录
     */
    @PreAuthorize("@ss.hasPermi('video:DSubgiftRecord:add')")
    @Log(title = "合伙人转增记录", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody DSubgiftRecord dSubgiftRecord)
    {
        if(StringUtils.isBlank(dSubgiftRecord.getHid())){
            throw new ServiceException("合伙人id不能为空");
        }
        if(StringUtils.isBlank(dSubgiftRecord.getUid())){
            throw new ServiceException("会员id不能为空");
        }

        //判断会员是否存在
        DUser hehuoren= idUserService.selectDUserById(dSubgiftRecord.getHid());
        if(hehuoren == null){
            throw new ServiceException("合伙人不存在");
        }

        if(!hehuoren.getLevel().equals("5")){
            throw new ServiceException("当前会员非合伙人,不能进行转赠");
        }

        DUser huiyuan= idUserService.selectDUserById(dSubgiftRecord.getUid());
        if(hehuoren == null){
            throw new ServiceException("会员不存在");
        }
        //判断当前充值金额与合伙人钱包余额
        if(hehuoren.getBalance().compareTo(dSubgiftRecord.getAmount()) == -1){
            throw new ServiceException("合伙人余额不足");
        }
        return toAjax(dSubgiftRecordService.insertDSubgiftRecord(dSubgiftRecord));
    }

    /**
     * 修改合伙人转增记录
     */
    @PreAuthorize("@ss.hasPermi('video:DSubgiftRecord:edit')")
    @Log(title = "合伙人转增记录", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody DSubgiftRecord dSubgiftRecord)
    {
        return toAjax(dSubgiftRecordService.updateDSubgiftRecord(dSubgiftRecord));
    }

    /**
     * 删除合伙人转增记录
     */
    @PreAuthorize("@ss.hasPermi('video:DSubgiftRecord:remove')")
    @Log(title = "合伙人转增记录", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable String[] ids)
    {
        return toAjax(dSubgiftRecordService.deleteDSubgiftRecordByIds(ids));
    }
}
